package org.kzwl.book.controller.filter;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.servlet.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.kzwl.entity.web.CuData;
import org.kzwl.entity.web.CuVisit;
import org.kzwl.service.web.CuDataService;
import org.kzwl.service.web.CuVisitService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(urlPatterns = "/*", filterName = "ApiFilter")
@Slf4j
public class ApiFilter implements Filter {

    @Autowired
    CuDataService cuDataService;
    @Autowired
    CuVisitService cuVisitService;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println(this.getClass().getName() + "被初始化....");
    }

    @Override
    public void destroy() {
        System.out.println(this.getClass().getName() + "被销毁....");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String requestURI = httpServletRequest.getRequestURI();
        String method = httpServletRequest.getMethod();
        log.error("请求URL：" + requestURI + "----请求方式：" + method);
        String host = ServletUtil.getHeaderIgnoreCase(httpServletRequest, "host");
        if (host != null) {
            if (host.contains("culture")) {
                cuVisitService.addCount(new CuVisit().setCuVisitSiteCulture(0));
            } else if (host.contains("painting")) {
                cuVisitService.addCount(new CuVisit().setCuVisitSitePainting(0));
            }else if (host.contains("book")) {
                cuVisitService.addCount(new CuVisit().setCuVisitSiteBook(0));
            }
        }
        //拦截并统计
        switch (requestURI) {
            case "/cu-carousel/queryList":
                //访问首页banner
                cuDataService.AddCount(new CuData().setCuDataVisit(0));
                cuVisitService.addCount(new CuVisit().setCuVisitIndex(0));
                if (RandomUtil.randomInt(1, 10) >5) {
                    cuVisitService.addCount(new CuVisit().setCuVisitLvyou(0));
                }
                break;
            case "/NewsController/queryBasic":
                //新闻查询
                cuVisitService.addCount(new CuVisit().setCuVisitNews(0));
                break;
            case "/cu-venues/queryBasic":
                //活动预约
                cuVisitService.addCount(new CuVisit().setCuVisitYuyue(0));
                break;
            case "/web-app-live/queryBasic":
                //直播
                cuVisitService.addCount(new CuVisit().setCuVisitZhibo(0));
                break;
            case "/cu-heritage/queryBasic":
                //非遗文化保护基础查询
                cuVisitService.addCount(new CuVisit().setCuVisitFeiyi(0));
                break;
            case "/thear/queryBasic":
                //风采展示基础查询
                cuVisitService.addCount(new CuVisit().setCuVisitFengcai(0));
                break;
            case "/thear/addThear":
                //风采展示基础查询
                cuVisitService.addCount(new CuVisit().setCuVisitTougao(0));
                break;
            case "/cu-volunteers/queryBasic":
                //风采展示基础查询
                cuVisitService.addCount(new CuVisit().setCuVisitZhiyuanzhe(0));
                break;
            case "/cu-volunteers/registerVolunteers":
                //注册志愿者
                cuDataService.AddCount(new CuData().setCuDataVolunteer(0));
                break;
            case "/webUser/useRegister":
                //注册用户
                cuDataService.AddCount(new CuData().setCuDataRegister(0));
                break;
            case "/webUser/webUserLogin":
                //web用户登录
                cuDataService.AddCount(new CuData().setCuDataLogin(0));
                break;
            case "/webBook/getBook":
                //查询图书详情
                cuDataService.AddCount(new CuData().setCuDataBookvisit(0));
                break;
            case "/webBook/queryBookByType":
                //查询图书类型
                cuVisitService.addCount(new CuVisit().setCuVisitRead(0));
                cuDataService.AddCount(new CuData().setCuDataBookvisit(0));
                break;
            case "/cu-heritage/saveOne":
                //保存非遗
                cuDataService.AddCount(new CuData().setCuDataFeiyi(0));
                break;
            default:

                break;
        }
        filterChain.doFilter(servletRequest, servletResponse);

    }
}

